﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DAL;
using System.Text;
using System.Data.SqlClient;
using DevExpress.Web.ASPxEditors;
namespace EDMS.Admin.DelegateRecord
{
    public partial class RecordManagers : System.Web.UI.Page
    {
        public static string Jlxh;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //判断用户是否已经登录
                try
                {
                    if (!CMSUser.Current.IsLogin)
                    {
                        Response.Redirect("~/Login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("~/Login.aspx");
                }

                if (CMSUser.Current.IsAdmin == true)
                {
                    cmdxjmc.SelectedIndex = 0;
                    cmdxjmc.ReadOnly = true;
                }
                Random ran = new Random();
                int value = ran.Next(1000, 10000);
                DateTime now = DateTime.Now;
                string yzm = value.ToString();
                Jlxh = now.ToString("yyyyMMddHHmmssfff") + yzm;
                lblyzm.Text = value.ToString();
                txtwtfs.ReadOnly = true;
                lblyzm.ReadOnly = true;
                txtfssj.ReadOnly = true;

                lbltime.Text = now.ToString();      //当前操作时间
                lblczr.Text = CMSUser.Current.Name; //当前操作人
                txtfssj.Text = now.ToString();      //委托发生日期
                //查询选举名称
                using (DataClassesDataContext lqdb = new DataClassesDataContext())
                {
                    string xjbh = Session["xjbh"].ToString();

                    var r = from o in lqdb.Xjdj
                            where o.xjbh == xjbh
                            select o;
                    foreach (var item in r)
                    {
                        
                        cmdxjmc.DataSourceID = "";
                        cmdxjmc.Items.Clear();
                        ListEditItem list = new ListEditItem();
                        list.Text = item.xjmc + "(编号：" + xjbh + ")";
                        list.Value = item.xjbh;
                        cmdxjmc.Items.Add(list);
                        cmdxjmc.SelectedIndex = 1;
                    }
                }
            }
        }
        //判断是否为空
        private bool isEmpty()
        {
            bool isSucceed = true;
            if (string.IsNullOrEmpty(txtwtname.Text))
            {
                Frame.Global.GlobalFunctions.Alert("请输入委托人姓名！");
                isSucceed = false;
            }
            if (string.IsNullOrEmpty(txtwtrsf.Text))
            {
                Frame.Global.GlobalFunctions.Alert("请输入委托人身份证号码！");
                isSucceed = false;
            }
            if (string.IsNullOrEmpty(txtwtPhone.Text))
            {
                Frame.Global.GlobalFunctions.Alert("请输入委托人联系方式！");
                isSucceed = false;
            }
            if (string.IsNullOrEmpty(txtbwtname.Text))
            {
                Frame.Global.GlobalFunctions.Alert("请输入被委托人姓名！");
                isSucceed = false;
            }
            if (string.IsNullOrEmpty(txtbwtsf.Text))
            {
                Frame.Global.GlobalFunctions.Alert("请输入被委托人身份证号码！");
                isSucceed = false;
            }
            if (string.IsNullOrEmpty(txtsm.Text))
            {
                Frame.Global.GlobalFunctions.Alert("请输入工作记录说明！");
                isSucceed = false;
            }
            return isSucceed;
        }
        //验证输入委托人信息是否正确
        bool isWtRight()
        {
            bool isright = true;
            string wtname = txtwtname.Text;   //委托人姓名
            string wtcard = txtwtrsf.Text;     //委托人身份证号码
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                var result = lqDB.Xm.FirstOrDefault(o => o.xmxm == wtname && o.sfzh == wtcard);
                if (result == null)
                {
                    Frame.Global.GlobalFunctions.Alert("对不起，您输入的委托人信息有误！");
                    txtwtname.Text = "";
                    txtwtrsf.Text = "";
                    isright = false;
                }
            }
            return isright;
        }
        //验证输入被委托人信息是否正确
        bool isBWtRight()
        {
            bool isright = true;
            string bwtname = txtbwtname.Text;  //被委托人姓名
            string bwtcard = txtbwtsf.Text;    //被委托人身份证号码
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                var result = lqDB.Xm.FirstOrDefault(o => o.xmxm == bwtname && o.sfzh == bwtcard);
                if (result == null)
                {
                    Frame.Global.GlobalFunctions.Alert("对不起，您输入的被委托人信息有误！");
                    txtbwtname.Text = "";
                    txtbwtsf.Text = "";
                    isright = false;
                }
            }
            return isright;

        }
        //添加电话受理记录
        void Add()
        {
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                if (isEmpty() == true)
                {
                    Random ran = new Random();
                    int value = ran.Next(1000, 10000);
                    DateTime now = DateTime.Now;
                    string yzm = value.ToString();
                    string str = now.ToString("yyyyMMddHHmmssfff");
                    string jlxh = str + yzm;

                    //验证用户输入的信息是否正确


                    if (isWtRight() == true && isBWtRight() == true)
                    {
                        Wtjl record = new Wtjl();
                        record.jlbh = Jlxh;
                        record.xjbh = Session["xjbh"].ToString();        //选举编号
                        record.wtname = txtwtname.Text;    //委托人姓名
                        record.wtrsfzh = txtwtrsf.Text;    //委托人身份证号
                        record.bwtname = txtbwtname.Text;  //被委托人姓名
                        record.bwtrsfzh = txtbwtsf.Text;  //被委托人身份证号
                        record.wtrmobile = txtwtPhone.Text;                   //委托人联系方式
                        record.fssj = Convert.ToDateTime(txtfssj.Text);        //委托发生时间
                        if (!cmdprint.Text.Equals(""))
                        {
                            record.wtsdysj = Convert.ToDateTime(cmdprint.Text);     //委托书打印时间
                        }
                        record.wtfs = "D";                            //委托方式
                        record.yzm = lblyzm.Text;                     //验证码
                        record.flag = '9';                            //委托状态  成功
                        record.issl = '9';                            //受理   成功
                        record.ishf = '1';                            //回访   待回访
                        lqDB.Wtjl.InsertOnSubmit(record);
                        lqDB.SubmitChanges();
                        //工作记录录入
                        InsertSljl();

                        #region//操作成功后被委托人的剩余被委托次数减一
                        using (DataClassesDataContext lq = new DataClassesDataContext())
                        {
                            var xmResult = lq.Xm.FirstOrDefault(r => r.sfzh == txtbwtsf.Text);
                            if (xmResult != null)
                            {
                                xmResult.sybwtcs = xmResult.sybwtcs - 1;
                                lq.SubmitChanges();
                            }
                        }
                        #endregion
                        Response.Write("<script language='javascript'>alert('操作成功！');window.close()</script> } ");
                    }


                }

            }
        }
        //插入受理记录说明
        void InsertSljl()
        {

            using (DataClassesDataContext lqdb = new DataClassesDataContext())
            {
                Sljl s = new Sljl();
                s.sm = txtsm.Text;
                //s.fssj = Convert.ToDateTime(lbltime.Text);
                s.fssj = DateTime.Now;
                s.czr = lblczr.Text;
                s.wtjlbh = Jlxh;
                lqdb.Sljl.InsertOnSubmit(s);
                lqdb.SubmitChanges();
            }
        }
        //管理
        protected void btnManage_Click(object sender, EventArgs e)
        {
            Add();
        }
        //委托人查询
        protected void gvwtShow_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            string str = e.Parameters;
            string wtnametext = txtxmxms.Text;
            string wtidcardtext = txtxmcards.Text;
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strSql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strSql = "select * from Xm where {0} xjbh='" + CurrentXjbh + "'";
            }
            else
            {
                strSql = "select * from Xm where {0} 1=1";
            }
            StringBuilder sb = new StringBuilder();
            List<SqlParameter> paralist = new List<SqlParameter>();
            if (!string.IsNullOrEmpty(wtnametext))
            {
                sb.Append("xmxm like '%'+ @name +'%' and ");
                paralist.Add(new SqlParameter("@name", wtnametext));
            }
            if (!string.IsNullOrEmpty(wtidcardtext))
            {
                sb.Append("sfzh like '%'+ @idcard +'%' and ");
                paralist.Add(new SqlParameter("@idcard", wtidcardtext));
            }
            string strsql = string.Format(strSql, sb.ToString());
            if (string.IsNullOrEmpty(wtnametext) && string.IsNullOrEmpty(wtidcardtext))
            {
                strsql = "select * from Xm where xmxm='xxxxxxxxxxxxxx'";
            }
            gvwtShow.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql, paralist.ToArray());
            gvwtShow.DataBind();
        }
        //被委托人查询
        protected void gvbwtShow_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            string str = e.Parameters;
            string wtnametext = txtxmxmb.Text;
            string wtidcardtext = txtxmcardb.Text;
            string CurrentXjbh = Session["Xjbh"].ToString();
            string strSql = "";
            if (CMSUser.Current.IsAdmin == true)
            {
                strSql = "select * from Xm where {0} xjbh='" + CurrentXjbh + "'";
            }
            else
            {
                strSql = "select * from Xm where {0} 1=1";
            }
            StringBuilder sb = new StringBuilder();
            List<SqlParameter> paralist = new List<SqlParameter>();

            if (!string.IsNullOrEmpty(wtnametext))
            {
                sb.Append("xmxm like '%'+ @name +'%' and ");
                paralist.Add(new SqlParameter("@name", wtnametext));
            }

            if (!string.IsNullOrEmpty(wtidcardtext))
            {
                sb.Append("sfzh like '%'+ @idcard +'%' and ");
                paralist.Add(new SqlParameter("@idcard", wtidcardtext));
            }
            string strsql = string.Format(strSql, sb.ToString());
            if (string.IsNullOrEmpty(wtnametext) && string.IsNullOrEmpty(wtidcardtext))
            {
                strsql = "select * from Xm where xmxm='xxxxxxxxxx'";
            }
            gvbwtShow.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql, paralist.ToArray());
            gvbwtShow.DataBind();
        }
        //委托人
        protected void btnClearwt_Click(object sender, EventArgs e)
        {
            if (!txtxmxms.Text.Equals("") && !txtxmcards.Text.Equals(""))
            {
                txtwtname.Text = txtxmxms.Text;
                txtwtrsf.Text = txtxmcards.Text;
            }
        }
        //被委托人
        protected void btnClear_Click(object sender, EventArgs e)
        {
            if (!txtxmxmb.Text.Equals("") && !txtxmcardb.Text.Equals(""))
            {
                txtbwtname.Text = txtxmxmb.Text;
                txtbwtsf.Text = txtxmcardb.Text;
            }
        }
    }
}